<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:permit:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:permit:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:permit:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['enterprise:permit:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="permitList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="排污许可证发证日期" prop="swCertificateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.swCertificateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排污许可证有效日" prop="swEffectiveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.swEffectiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排污许可证正本" prop="swOriginal">
        <template slot-scope="scope" v-if="scope.row.swOriginal">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="`${baseUrl}${scope.row.swOriginal}`"  style="height: 300px;width: 300px"/>
            <img slot="reference" :src="`${baseUrl}${scope.row.swOriginal}`"  :alt="scope.row.swOriginal" style="height: 15px;width: 15px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排污许可证副本" prop="swOriginalCopy">
        <template slot-scope="scope"  v-if="scope.row.swOriginalCopy">
          <el-popover placement="right" title="" trigger="hover">
            <img :src="`${baseUrl}${scope.row.swOriginalCopy}`"  style="height: 300px;width: 300px"/>
            <img slot="reference" :src="`${baseUrl}${scope.row.swOriginalCopy}`"  :alt="scope.row.swOriginalCopy" style="height: 15px;width: 15px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排污许可证类型" prop="swType"/>
      <el-table-column align="center" label="排污许可证发证机关" prop="swLicenseIssuingAgencies"/>
      <el-table-column align="center" label="许可证编号" prop="swCode"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['enterprise:permit:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['enterprise:permit:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />


    <!-- 添加或修改排污许可证对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="clearfix ">
        <div class="content-left" style="margin-left:90px;width: 40%">
            <el-form-item label="发证日期" prop="swCertificateDate">
              <el-date-picker v-model="form.swCertificateDate" clearable
                              placeholder="选择排污许可证发证日期"
                              size="small"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="有效日期" prop="swEffectiveDate">
              <el-date-picker v-model="form.swEffectiveDate" clearable
                              placeholder="选择排污许可证有效日期"
                              size="small"
                              type="date"
                              style="width: 100%"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          <el-form-item label="许可证发证机关" prop="swLicenseIssuingAgencies">
            <el-input v-model="form.swLicenseIssuingAgencies" placeholder="请输入排污许可证发证机关"/>
          </el-form-item>
          <el-form-item label="排污许可证类型" prop="swType">
            <el-input v-model="form.swType" placeholder="请选择排污许可证类型"/>
          </el-form-item>
          <el-form-item label="排污许可证种类" prop="swZl">
            <el-select v-model="form.swZl" placeholder="请选择" >
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="许可证编号" prop="swCode">
            <el-input v-model="form.swCode" placeholder="请输入许可证编号"/>
          </el-form-item>

        </div>
        <div class="content-left" style="margin-left: 90px;">
            <el-form-item label="排污许可证正本" prop="swOriginal">
              <ImageUpload v-model="form.swOriginal"/>
            </el-form-item>
            <el-form-item label="排污许可证副本（全本）上传" prop="swOriginalCopy">
              <FileUpload v-model="form.swOriginalCopy"/>
            </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addPermit, delPermit, exportPermit, getPermit, listPermit, updatePermit} from "@/api/enterprise/permit";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";
export default {
  name: "Permit",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排污许可证表格数据
      permitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        swCertificateDate: null,
        swEffectiveDate: null,
        swOriginal: null,
        swOriginalCopy: null,
        swType: null,
        swLicenseIssuingAgencies: null,
        swCode: null,
        swZl:null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      typeOptions:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("pwxkzl").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {

    /** 查询排污许可证列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          listPermit(this.queryParams).then(response => {
            this.permitList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        swId: null,
        swCertificateDate: null,
        swEffectiveDate: null,
        swOriginal: null,
        swOriginalCopy: null,
        swType: null,
        swLicenseIssuingAgencies: null,
        swCode: null,
        swZl:null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.swId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排污许可证";
      this.upload.fileList = [];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const swId = row.swId || this.ids
      getPermit(swId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排污许可证";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.swId != null) {
            updatePermit(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addPermit(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const swIds = row.swId || this.ids;
      this.$confirm('是否确认删除排污许可证编号为"' + swIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delPermit(swIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有排污许可证数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportPermit(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
  }
};
</script>
<style>
@import "../../../assets/styles/newAdd.css";
@import'../../../assets/styles/layui.css';
</style>
